import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_cwhisky/component/text_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../common/style/colors.dart';
import '../../common/style/images.dart';
import '../../component/app_bottom_bar.dart';
import '../../component/base/base_state_less_view.dart';
import '../../component/lazy_load_index_stack.dart';
import 'index_logic.dart';

class IndexPage extends BaseStateLessView<IndexLogic> {
  DateTime? lastPopTime;

  IndexPage({super.key});

  @override
  Widget buildContent(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          backgroundColor: AppColors.white,
          bottomNavigationBar: buildBottomNavigationBar2(),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
          floatingActionButton: GestureDetector(
            behavior: HitTestBehavior.opaque,
            child: Container(
              margin: EdgeInsets.only(top: 15.h),
              padding: EdgeInsets.only(top: 15.h),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Image(
                    image: const AssetImage(AppImages.tabAddSelected),
                    height: 39.r,
                    width: 39.r,
                    fit: BoxFit.cover,
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 4.h),
                    child: TextView(
                      text: '送拍',
                      fontColor: AppColors.color33,
                      fontSize: 10.sp,
                    ),
                  )
                ],
              ),
            ),
            onTap: () {
              //点击事件
            },
          ),
          body: LazyLoadIndexedStack(
            index: controller.index,
            children: controller.page,
          ),
        ),
        onWillPop: () async {
          // 点击返回键的操作
          if (lastPopTime == null || DateTime.now().difference(lastPopTime!) > const Duration(seconds: 2)) {
            lastPopTime = DateTime.now();
            Get.snackbar(
              "提示",
              "再按一次退出应用",
            );
            return false;
          } else {
            lastPopTime = DateTime.now();
            // 退出app
            SystemChannels.platform.invokeMethod('SystemNavigator.pop');
            return true;
          }
        });
  }

  buildBottomNavigationBar2() {
    return BottomAppBar(
      color: Colors.white,
      elevation: 10,
      shape: CustomShape(),
      child: AppBottomBar(
        iconList: <TabIconData>[
          TabIconData(title: '首页', icon: AppImages.tabHomeNormal, selectIcon: AppImages.tabHomeSelected),
          TabIconData(title: '分类', icon: AppImages.tabClassifyNormal, selectIcon: AppImages.tabClassifySelected),
          TabIconData(
            animate: false,
            icon: AppImages.tabAddSelected,
          ),
          TabIconData(title: '购物车', dot: 2, icon: AppImages.tabCartNormal, selectIcon: AppImages.tabCartSelected),
          TabIconData(title: '我的', icon: AppImages.tabMineNormal, selectIcon: AppImages.tabMineSelected),
        ],
        selectedPosition: controller.index,
        selectedCallback: (position) => controller.changeBottomBarIndex(position),
      ),
    );
  }

}

class CustomShape extends NotchedShape {
  @override
  Path getOuterPath(Rect host, Rect? guest) {
    Path path = Path();
    var pLeft1 = Offset(host.width / 2 - 45, host.top);
    var pLeft2 = Offset(host.width / 2 - 32, host.top);
    var pLeft3 = Offset(host.width / 2 - 24, host.top - 8);
    var pTop = Offset(host.width / 2, host.top - 28);
    var pRight1 = Offset(host.width / 2 + 24, host.top - 8);
    var pRight2 = Offset(host.width / 2 + 32, host.top);
    var pRight3 = Offset(host.width / 2 + 45, host.top);
    path
      ..moveTo(host.left, host.top)
      ..lineTo(pLeft1.dx, pLeft1.dy)
      ..quadraticBezierTo(pLeft2.dx, pLeft2.dy, pLeft3.dx, pLeft3.dy)
      ..quadraticBezierTo(pTop.dx, pTop.dy, pRight1.dx, pRight1.dy)
      ..quadraticBezierTo(pRight2.dx, pRight2.dy, pRight3.dx, pRight3.dy)
      ..lineTo(host.right, host.top)
      ..lineTo(host.right, host.bottom)
      ..lineTo(host.left, host.bottom)
      ..close();
    return path;
  }
}
